using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Timers;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Simnova.Collaborate.Presenter;
using Simnova.Collaborate.View;

public partial class _TimeEnter : System.Web.UI.Page, ILogin, ITimeLog
{
    private LoginPresenter _loginPresenter;
    private TimeLogPresenter _timeLogPresenter;
    private IList LogList;
    private int UserId;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            _loginPresenter = new LoginPresenter(this);
            LoginPanel.Visible = true;
        }
        else
        {
            UserId = (int)Session["UserId"];
            _timeLogPresenter = new TimeLogPresenter(this, UserId);
            
            LoggedInPanel.Visible = true;
            Page.ClientScript.RegisterClientScriptInclude("timer", "Timer.js");
            StartTimer.OnClientClick = "javascript:StartTimer(" + TimeElapsed.UniqueID + "," + MinutesElapsed.UniqueID + ");";
            StopTimer.OnClientClick = "javascript:PauseTimer();";
            AddEntry.OnClientClick = "javascript:PauseTimer();";
        }

    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        int userId = 0;
        try
        {
            LoginValidator.ErrorMessage = "";
            userId = _loginPresenter.Login();

        }
        catch (Exception ex)
        {
            if (ex.Message == "Login Failed")
            {
                LoginValidator.ErrorMessage = "Login Failed";
                LoginValidator.Enabled = true;
            }
            else
            {
                throw;
            }
            return;
        }
        Session["UserId"] = userId;
        Response.Redirect(@"~\TimeEnter.aspx", true);
    }



    protected void StartTimer_Click(object sender, EventArgs e)
    {
        SaveText.Text = "";
    }

    protected void AddEntry_Click(object sender, EventArgs e)
    {
        _timeLogPresenter.SaveTimeLogEntry();
        TimeComment.Text = "";
        SaveText.Text = "[Saved]";
    }

    #region ILogin Members

    string ILogin.EmailAddress
    {
        get
        {
            return EmailAddressTextBox.Text;
        }
        set
        {
            EmailAddressTextBox.Text = value;
        }
    }

    string ILogin.Password
    {
        get
        {
            return PasswordTextBox.Text;
        }
        set
        {
            PasswordTextBox.Text = value;
        }
    }

    #endregion

    #region ITimeLog Members

    string ITimeLog.Description
    {
        get { return TimeComment.Text; }
    }

    int ITimeLog.Duration
    {
        get {
            String tval = MinutesElapsed.Value;
            return int.Parse(MinutesElapsed.Value);
        }
    }

    System.Collections.IList ITimeLog.LogEntries
    {
        set { LogList = value; }
    }

    System.Collections.IList ITimeLog.Companies
    {
        set
        {
            //companyComboBox.DataSource = value;
            //companyComboBox.DisplayMember = "Name";
            //companyComboBox.ValueMember = "CompanyId";
        }
    }

    int ITimeLog.SelectedCompanyId
    {
        get
        {
            //if (companyComboBox.SelectedValue == null) return 0;
            //int companyId;
            //int.TryParse(companyComboBox.SelectedValue.ToString(), out companyId);
            //return companyId;
            return 0;
        }
    }

    System.Collections.IList ITimeLog.Roles
    {
        set
        {
            //roleComboBox.DataSource = value;
            //roleComboBox.DisplayMember = "Name";
            //roleComboBox.ValueMember = "RoleId";
        }
    }

    int ITimeLog.SelectedRoleId
    {
        get
        {
            //if (roleComboBox.SelectedValue == null) return 0;
            //int roleId;
            //int.TryParse(roleComboBox.SelectedValue.ToString(), out roleId);
            //return roleId;
            return 0;
        }
    }

    System.Collections.IList ITimeLog.ActivityTypes
    {
        set
        {
            //activityTypeComboBox.DataSource = value;
            //activityTypeComboBox.DisplayMember = "Name";
            //activityTypeComboBox.ValueMember = "ActivityTypeId";
        }
    }

    int ITimeLog.SelectedActivityTypeId
    {
        get
        {
            //if (activityTypeComboBox.SelectedValue == null) return 0;
            //int activityTypeId;
            //int.TryParse(activityTypeComboBox.SelectedValue.ToString(), out activityTypeId);
            //return activityTypeId;
            return 0;
        }
    }

    #endregion
}
